/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package isi.corba;

import isi.corba.idl.Enchere.Client;
import isi.corba.idl.Enchere.ClientHelper;
import isi.corba.idl.Enchere.GestClient;
import isi.corba.idl.Enchere.GestClientHelper;
import isi.corba.implementation.*;
import javax.persistence.EntityManager;
import org.omg.CORBA.ORB;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.NotFound;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
import org.omg.PortableServer.POAPackage.ServantNotActive;
import org.omg.PortableServer.POAPackage.WrongPolicy;

/**
 *
 * @author romain
 */
public class ServerMain {
  public static void mainserver(String args[], EntityManager em) throws AdapterInactive, InvalidName, ServantNotActive, WrongPolicy, org.omg.CosNaming.NamingContextPackage.InvalidName, NotFound, CannotProceed
  { 
	try
        {
      // initialisation de l’ORB et création de l’objet servant
	ORB orb = ORB.init(args, null);

	POA rootpoa = POAHelper.narrow (orb.resolve_initial_references("RootPOA"));

	rootpoa.the_POAManager().activate();

	ServerClient serverClientImpl = new ServerClient(orb,em);

        org.omg.CORBA.Object ref = rootpoa.servant_to_reference(serverClientImpl);
	GestClient cref = GestClientHelper.narrow(ref);

 	org.omg.CORBA.Object objRef =
		orb.resolve_initial_references("NameService");

	NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

	String name = "Enchere";
	NameComponent path[] = ncRef.to_name( name );
	ncRef.rebind(path, cref);

	System.out.println("EnchereServer ready and waiting ...");
	orb.run();
  }  catch (Exception e) { System.err.println("ERROR: " + e);}
  System.out.println("EnchereServer Exiting ...");
    }
}
